﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Model;
using Util;
using System.Web;

namespace BLL
{
    public class BllAdmin:Bll<tb_admin>,IAccount<tb_admin>
    {
        public BllAdmin() { }
        public BllAdmin(Page _page) : base(_page) { }
        public BllAdmin(Dal<tb_admin> _dal) : base(_dal) { }
        public BllAdmin(Page _page, Dal<tb_admin> _dal) : base(_page, _dal) { }
        public override bool Check(tb_admin entity)
        {
            return true;
        }

        public bool Loged
        {
            get
            {
                if (_page == null)
                    return false;
                var temp = _page.Request.Cookies[KoInfo.CookieAdmin];
                if (temp == null || string.IsNullOrWhiteSpace(temp.Value))
                    return false;
                else
                    return true;
            }
        }

        public bool Login(tb_admin admin)
        {
            bool _loged = false;
            if (admin.username.Equals("") || admin.pwd.Equals(""))
            {
                UtilWeb.Message(_page, KoInfo.LoginEmpty, null);
                return _loged;
            }
            _loged = (GetList(c => c.username.Equals(admin.username) && c.pwd.Equals(admin.pwd)).Count > 0);
            if (!_loged)
            {
                UtilWeb.Message(_page, KoInfo.LoginDismatch, null);
            }
            return _loged;
        }

        public bool Login(string username, string password)
        {
            tb_admin admin = new tb_admin();
            admin.username = username;
            admin.pwd = password;
            return Login(admin);
        }

        public bool Logout()
        {
            HttpCookie cookie = _page.Request.Cookies[KoInfo.CookieAdmin];
            cookie.Expires = DateTime.Now.AddDays(-1);
            _page.Response.Cookies.Add(cookie);
            return true;
        }

        public void GoLogin()
        {
            UtilWeb.Message(_page, null, "window.open('login.aspx','_parent')");
        }
    }
}
